package com.bk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bk.domain.Article;
import com.bk.mapper.ArticleMapper;
import com.bk.service.ArticleService;
import com.bk.utils.Result;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 24940
 * @description 针对表【article】的数据库操作Service实现
 * @createDate 2024-04-04 11:19:56
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article>
        implements ArticleService {

    @Override
    public Result addArticle(Article article) {
        article.setPublishTime(new Date());
        article.setPublishStatus("已发布");
        return Result.success(this.save(article));
    }

    @Override
    public Result draft(Article article) {
        article.setPublishTime(new Date());
        article.setPublishStatus("草稿");
        return Result.success(this.saveOrUpdate(article));
    }

    @Override
    public Result updateArt(Article article) {
        article.setPublishTime(new Date());
        article.setPublishStatus("已发布");
        return Result.success(this.saveOrUpdate(article));
    }

    @Override
    public Result getArticleByAuthorId(Integer authorId) {
        List<Article> query = list(new LambdaQueryWrapper<Article>().eq(Article::getAuthorId, authorId));
        return Result.success(query);
    }

    @Override
    public Result search(String keyword) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Article::getTitle, keyword).or().like(Article::getContent, keyword).or().like(Article::getDescription, keyword).or().like(Article::getAuthorName, keyword);
        return Result.success(list(queryWrapper));
    }
}




